package cn.haut.online_read.controller.sanwenzx;

import cn.haut.online_read.crawler.sanwenzx.CategoryCrawler;
import cn.haut.online_read.utils.CrawlerUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @author xing
 * @date 2021-05-21 20:10
 */
@Controller(value = "categoryNamesController")
@RequestMapping(value = "/sanwenzx")
public class CatagoryNamesController {

    public static final String BASE_URL = "http://sanwenzx.com";
    public static final String DEFAULT_URL = "http://sanwenzx.com/sanwen/";

    @GetMapping(value = {"/getCategory", "/{major}", "/{major}/"})
    @ResponseBody
    public String getCategory(
            @PathVariable(value = "major", required = false) String major
    ) {
        if (major == null) {
            return CrawlerUtils.getJsonResult(new CategoryCrawler(), DEFAULT_URL);
        } else {
            return CrawlerUtils.getJsonResult(new CategoryCrawler(), BASE_URL + "/" + major);
        }
    }
}
